/*
  Copyright © 2011  Vinícius dos Santos Oliveira

  This file is part of Vinipsmaker's HTTP tools.

  Vinipsmaker's HTTP tools is free software: you can redistribute it and/or
  modify it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or (at your
  option) any later version.

  This program is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  details.

  You should have received a copy of the GNU General Public License along with
  this program.  If not, see <http://www.gnu.org/licenses/>.
  */

#include "dnsinfowindow.h"
#include "ui_dnsinfowindow.h"
#include <QHostInfo>
#include <QMessageBox>

DnsInfoWindow::DnsInfoWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::DnsInfoWindow),
    prevLookup(false)
{
    ui->setupUi(this);
}

DnsInfoWindow::~DnsInfoWindow()
{
    delete ui;
}

void DnsInfoWindow::on_searchButton_clicked()
{
    ui->listWidget->clear();

    if (prevLookupId) {
        QHostInfo::abortHostLookup(prevLookupId);
    }

    prevLookupId = QHostInfo::lookupHost(ui->lineEdit->text(), this, SLOT(onHostInfo(QHostInfo)));
    prevLookup = true;

    ui->statusbar->showMessage(tr("Host lookup initiated"), 1000);
}

void DnsInfoWindow::onHostInfo(const QHostInfo &host)
{
    prevLookup = false;

    if (host.error() != QHostInfo::NoError) {
        QMessageBox::information(this, tr("An error occurred"), host.errorString());
        return;
    }

    ui->statusbar->showMessage(tr("Host found"), 1000);

    foreach (const QHostAddress &addr, host.addresses())
        ui->listWidget->addItem(addr.toString());
}
